package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.watson.R;
import com.watson.bean.ModuleBean;

import java.util.ArrayList;
import java.util.Map;


public class ModuleAdapter extends RecyclerView.Adapter<ModuleAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private ArrayList<Map> cornermark = new ArrayList<>();
    private ArrayList<Map> cornermark1 = new ArrayList<>();

    public ModuleAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void cornermark(ArrayList<Map> cornermark) {
        this.cornermark = cornermark;
        notifyDataSetChanged();
    }

    public void cornermark1(ArrayList<Map> cornermark1) {
        this.cornermark1 = cornermark1;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_module, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvTitle.setText(mList.get(position).get("funsname") + "");

            ArrayList<Map> dataList = new ArrayList<>();
            dataList = (ArrayList<Map>) JSON.parseArray(mList.get(position).get("funLists") + "", Map.class);
            if (dataList.size() > 0) {
                ArrayList<Map> cornermarkAll = new ArrayList<>();
                GridLayoutManager gridManager = new GridLayoutManager(mContext, 3, RecyclerView.VERTICAL, false);
                holder.recyclerData.setLayoutManager(gridManager);
                Module2Adapter module2Adapter = new Module2Adapter(mContext, dataList);
                holder.recyclerData.setAdapter(module2Adapter);
                if (cornermark.size() > 0) {
                    cornermarkAll.addAll(cornermark);
                }
                if (cornermark1.size() > 0) {
                    cornermarkAll.addAll(cornermark1);
                }
                module2Adapter.cornermark(cornermarkAll);
                module2Adapter.setItemClickListener((position1, mList1) -> {
                    if (mItemClick != null) {
                        mItemClick.onClick(position1, mList1);
                    }
                });
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tvTitle;
        private ImageView imgId;
        private RecyclerView recyclerData;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvName = itemView.findViewById(R.id.tvName);
            imgId = itemView.findViewById(R.id.imgId);
            recyclerData = itemView.findViewById(R.id.recyclerData);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, Map mList);
    }
}
